function createBarchart(container,p_data,p_json,svg,projection) {
	$("#"+container).empty();
	d3.json(mapJSON, function(json) {
		var isArray = false;
		if(p_data[0].properties.attribute[1][1] != undefined)
			isArray = true;
		if(isArray)
			var aggregatedData = aggregateDate(p_data, true);
		else
			var aggregatedData = aggregateDate(p_data);
		var data = [];

		for(n in aggregatedData){
			var sum = 0;
			var label = null;
			for(i = 0; i < aggregatedData[n].length; i++) {
				if(isArray)
					sum+= aggregatedData[n][i][1];
				else
					sum += aggregatedData[n][i];
			}	
			if(n.split("/")[1] != undefined)
				label = getMonth(n.split("/")[1]);
			else
				label = n;
			var obj = {"label":label,"timestamp":n,"value":sum};
			data.push(obj);
		}
		
		var max = 0;
		for(i = 0; i < data.length; i++) {
			if(data[i].value > max)
				max = data[i].value;
		}
		
		var w = 35;
		var h = 140;
		
		var x = d3.scale.linear()
		.domain([ 0, 1 ])
		.range([ 0, w ]);
	
		var y = d3.scale.linear()
		.domain([ 0, max ])
		.rangeRound([ 0, h ]);
		
		var chart = d3.select("#"+container).append("svg")
						.attr("class", "chart")
						.attr("width", (w+5) * data.length - 1)
						.attr("height", h);	
		
		chart.selectAll("rect")
			.data(data)
			.enter().append("rect")
				.attr("x", function(d, i) { return x(i) - .5 + 40; })
				.attr("y", function(d) { return h -20 - y(d.value) - .5; })
				.attr("width", w)
				.attr("height", function(d) { return y(d.value); })
				.on("click", function(o) { 
					if(!isArray)
						plotLocations(p_json,svg,projection,o.timestamp);
				 	timestamp = o.timestamp;
				 	$(".chart rect").css("fill","#6C8CD5");
				 	$(this).css("fill","#FFA12F");
					if(isArray)
						createPiechart("piechart",p_json,timestamp,kpi);
					else
						createPiechart("piechart",p_json,timestamp);
				})
				.on("mouseover", function(o) { 
					$(this).css("fill-opacity","1");
					if(!isArray)
						plotLocations(p_json,svg,projection,o.timestamp);
					else
						plotDataStreams(p_json,svg,projection,o.timestamp);
				}) 			
				.on("mouseout", function(o) {
					$(this).css("fill-opacity","0.7");	
				}) 
			;
		
		if(monthIndicator == true)
			$($(".chart rect").get(0)).css("fill","#FFA12F");
		else
			$($(".chart rect").get($(".chart rect").size()-1)).css("fill","#FFA12F");
	
		$(".chart").hover(function(){},function(){
			if(!isArray)
				plotLocations(p_json,svg,projection,timestamp);
			else
				plotDataStreams(p_json,svg,projection,timestamp);
		});
		
		//y-axis
		chart.append("svg:line")
	    .attr("class", "axes")
	    .attr("x1", 40)
	    .attr("x2", 40)
	    .attr("y1", 0)
	    .attr("y2", h -19);
		
		//x-axis
		chart.append("svg:line")
	    .attr("class", "axes")
	    .attr("x1", 39)
	    .attr("x2", w*data.length  + 39)
	    .attr("y1", h - 20)
	    .attr("y2", h - 20);
	
		//labels x-axis
		for(i = 0; i < data.length; i++) {
			chart.append("svg:text")
	        .attr("class", "axislabel")
	        .attr("x", (i+1.15)*35+"px")
	        .attr("y",  "95%")
	        .attr("dy", 4)
	        .attr("text-anchor", "left")
	        .text(data[i].label);
		}
		
		//labels y-axis
		var labelx = null;
		var labely = null;
		var text = null;
		for(i = 0; i < 3; i++) {
			switch (i) {
				case 0:
					labelx = "6%";
					labely = "85%";
					text = 0;
					break;
				case 1:
					labelx = "0%";
					labely = "45%";
					text = max/2;
					break;
				case 2:
					labelx = "0%";
					labely = "3%";
					text = max;
					break;
			}
			chart.append("svg:text")
	        .attr("class", "axislabel")
	        .attr("x", labelx)
	        .attr("y", labely)
	        .attr("dy", 4)
	        .attr("text-anchor", "right")
	        .text(numToStr(text));
		}
	});	
}

function createPiechart(container,p_json,p_timestamp,p_kpi) {
	$("#"+container).empty();
	d3.json(p_json, function(json) {
		data = [];
		json.data.forEach(function(o) {
	        if(o.properties.timestamp == p_timestamp) {
	        	if(p_kpi != undefined) {
	        		if(o.properties.attribute[1][1] == p_kpi) {
	    				var obj = {"name":o.properties.sender,"attribute":p_kpi,"value":$(o.properties.value).get(1)};
	    				data.push(obj);
	        		}
	        	} else {
					var obj = {"name":o.properties.name,"attribute":o.properties.attribute,"value":o.properties.value};
					data.push(obj);
	        	}
	        }
		});	
		
		var w = 200,                       
	    h = 200,                          
	    r = 85;
		var color = d3.scale.ordinal()  
//		.range(["#DAE2F4", "#B6C6EA", "#6C8CD5","#51699F","#36466A"]); 
		.range(["#DAE2F4", "#FDCE95", "#B6C6EA","#FFB965","#6C8CD5","#FFA12F","#51699F","#F5862B","#36466A","#E46C0A"]); 
//	    color = d3.scale.category20c();     
		
	    var vis = d3.select("#"+container)
	        .append("svg:svg")              
	        .data([data])                   
	            .attr("width", w)           
	            .attr("height", h)
	        .append("svg:g")                
	            .attr("transform", "translate(" + r + "," + r + ")")    
	
	    var arc = d3.svg.arc()              
	        .outerRadius(r);
	
	    var pie = d3.layout.pie()          
	        .value(function(d) { return d.value; });   
	
	    var arcs = vis.selectAll("g.slice")     
	        .data(pie)                          
	        .enter()                           
	            .append("svg:g")               
	                .attr("class", "slice");   
	
	        arcs.append("svg:path")
	                .attr("fill", function(d, i) { return color(i); } ) 
	                .attr("fill-opacity","0.7")
	                .attr("d", arc)
	                .attr("class","pieSlice")
					.on("mouseover", function(o){
						$(".infoPopup").remove();
						if($('.infoPopup').length == 0) {
							drawLocationPopup(o,d3.event);
							$(".infoPopup").fadeIn();
						} 
					 })
					.on("mouseout", function(){
							var popupToClose = $(".infoPopup");
							setTimeout(function(){
								if(infoPopup == false) {
									popupToClose.fadeOut('normal'); 
								}	
							}, 500);						
						})
				     ;
	});
}

function aggregateDate(data,stream){
	var res = new Object();
	for(i = 0; i < data.length; i++) {
		if(stream != undefined) {
			if(searchArrayKey(res,data[i].properties.timestamp.toString())) {
				if(data[i].properties.attribute[1][1] == kpi)
					res[data[i].properties.timestamp.toString()].push(data[i].properties.value);
			} else {
				if(data[i].properties.attribute[1][1] == kpi) {
					res[data[i].properties.timestamp.toString()] = [];
					res[data[i].properties.timestamp.toString()].push(data[i].properties.value);					
				}
			} 				
		} else {
			if(searchArrayKey(res,data[i].properties.timestamp.toString())) {
				res[data[i].properties.timestamp.toString()].push(data[i].properties.value);
			} else {
				res[data[i].properties.timestamp.toString()] = [];
				res[data[i].properties.timestamp.toString()].push(data[i].properties.value);
			} 	
		}
		
		
		
	}
	return res;
}

function searchArrayValue(array,value){
	var result = false;
	$(array).each(function(index,val){
		if(val == value) {
			result = true;
			return false;
		}
	});
	return result;
}

function searchArrayKey(array,key){
	if(array[key] != undefined)
		return true;
	return false;
}

function capitaliseFirstLetter(string){
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function getColor(i){
	var colors = ["#6C8CD5","#EBAC13"];
	return colors[i];
}

function round(x) {
	  var e = Math.pow(10, 1);
	  var k = (Math.round(x * e) / e).toString();
	  if (k.indexOf('.') == -1) k += '.';
	  k += e.toString().substring(1);
	  return k.substring(0, k.indexOf('.') + 1+1);
	}

function numToStr(val) {
	var number = parseInt(val);
	
	if(number > 9999 && number < 100000)
		return round(number/1000) +"K";
	else if(number > 99999 && number < 1000000)
		return round(number/1000) +"K";
	else if(number > 999999 && number < 1000000000)
		return round(number/1000000) +"M";
	else if(number > 9999999)
		return round(number/10000000) +"B";
	else
		return number;
}

function getMonth(month) {
	var monthArray = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	return monthArray[month-1];
}